package com.yoda.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.constant.MessageConstant;
import com.yoda.dto.GetDto;
import com.yoda.dto.ListDto;
import com.yoda.entity.YddLink;
import com.yoda.mapper.YddLinkMapper;
import com.yoda.vo.DataPage;
import com.yoda.result.Result;
import com.yoda.service.IYddLinkService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-05-15
 */
@Service
public class YddLinkServiceImpl extends ServiceImpl<YddLinkMapper, YddLink> implements IYddLinkService {

    /**
     * 链接详情
     *
     * @param
     */
    @Override
    public Result get(Integer id) {
        //1.非空判断
        if (id==null){
            return  Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //2.调dao层
        YddLink yddLink = this.getById(id);
        return Result.success(yddLink);
    }

    /**
     * 链接修改
     *
     * @param dto
     * @return
     */
    @Override
    public Result getupdate(GetDto dto) {
        //1.非空判断
        if (dto.getId()==null){
            return  Result.error(MessageConstant.ID_NOT_FOUND);
        }
        YddLink yddLink=new YddLink();
        BeanUtils.copyProperties(dto,yddLink);
        if (yddLink.getId()==null){
            yddLink.setId(Integer.valueOf(dto.getId()));
        }
         this.updateById(yddLink);
        return Result.success();
    }

    /**
     * GET 链接删除
     *
     * @param id
     */
    @Override
    @Transactional
    public Result del(String id) {
        if (id==null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        this.removeById(id);
        return Result.success();
    }


    /**
     * 链接添加
     *
     * @param dto
     */
    @Override
    public void save(GetDto dto) {
        YddLink yddLink=new YddLink();
        BeanUtils.copyProperties(dto,yddLink);
        this.save(yddLink);
    }

    /**
     * 链接列表
     *
     * @param dto
     * @return
     */
    @Override
    public DataPage give(ListDto dto) {
        LambdaQueryWrapper<YddLink> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddLink::getType, dto.getType());

        IPage<YddLink> pageplus = new Page<>(Integer.valueOf(dto.getPage()), Integer.valueOf(dto.getLimit()));
        IPage<YddLink> page = this.page(pageplus, lqw);

        DataPage dataPage = new DataPage();
        dataPage.setCount(page.getTotal());
        dataPage.setPageSize(page.getSize());
        dataPage.setCurrentPage(page.getCurrent());
        dataPage.setTotalPages(page.getPages());
        dataPage.setData(page.getRecords());

        return dataPage;
    }

}
